/***
 * Copyright (c) 2010 readyState Software Ltd
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may obtain
 * a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */

package com.detroitchowfree.mapviewballoons;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View.OnClickListener;

import com.detroitchowfree.DetroitChowActivity;
import com.detroitchowfree.IFetchStoresObserver;
import com.detroitchowfree.LogUtil;
import com.detroitchowfree.MyItemizedOverlay;
import com.detroitchowfree.MyOverlayItem;
import com.detroitchowfree.R;
import com.detroitchowfree.StoreService;
import com.detroitchowfree.ReviewActivity;
import com.detroitchowfree.StoreVO;
import com.google.android.maps.OverlayItem;
import com.detroitchowfree.ReviewActivity;;

/**
 * A view representing a MapView marker information balloon.
 * <p>
 * This class has a number of Android resource dependencies:
 * <ul>
 * <li>drawable/balloon_overlay_bg_selector.xml</li>
 * <li>drawable/balloon_overlay_close.png</li>
 * <li>drawable/balloon_overlay_focused.9.png</li>
 * <li>drawable/balloon_overlay_unfocused.9.png</li>
 * <li>layout/balloon_map_overlay.xml</li>
 * </ul>
 * </p>
 * 
 * @author Jeff Gilfelt
 *
 */
public class BalloonOverlayView<Item extends MyOverlayItem> extends FrameLayout 
{

	private LinearLayout layout;
	private TextView title;
	private TextView snippet;
	private Button btnNav;
	private int storeId;
	private Context myContext;
	private StoreService storeService;
	Intent intent;
	
	/**
	 * Create a new BalloonOverlayView.
	 * 
	 * @param context - The activity context.
	 * @param balloonBottomOffset - The bottom padding (in pixels) to be applied
	 * when rendering this view.
	 */
	public BalloonOverlayView(Context context, int balloonBottomOffset) {
		super(context);
		myContext = context;
		setPadding(10, 0, 10, balloonBottomOffset);
		layout = new LinearLayout(context);
		layout.setVisibility(VISIBLE);

		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View v = inflater.inflate(R.layout.balloon_overlay, layout);
		title = (TextView) v.findViewById(R.id.balloon_item_title);
		snippet = (TextView) v.findViewById(R.id.balloon_item_snippet);

		ImageView close = (ImageView) v.findViewById(R.id.close_img_button);
		close.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				layout.setVisibility(GONE);
			}
		});
		
	 Button btnNav = (Button) v.findViewById(R.id.btnNav);
	 btnNav.setOnClickListener(navButtonClickListener);
	 
	 Button btnPhone = (Button) v.findViewById(R.id.btnPhone);
	 btnPhone.setOnClickListener(phoneButtonClickListener);
	 
	 Button btnComment = (Button) v.findViewById(R.id.btnComment);
	 btnComment.setOnClickListener(reviewsClickListener);
	 
	 Button btnWeb = (Button) v.findViewById(R.id.btnWeb);
	 btnWeb.setOnClickListener(webClickListener);
		
		//LogUtil.log("BalloonOverlayView", "constructor");
		/*
	 LogUtil.log("BalloonOverlayView", "vefore");
	 
	 LogUtil.log("BalloonOverlayView", "btnNav " + btnNav.getText());
	 ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(200, 200);
	 LogUtil.log("BalloonOverlayView", "LayoutParams");
	 btnNav.setLayoutParams(layoutParams);
	 LogUtil.log("BalloonOverlayView", "After Layout Parmas");
	 */
		
		
		FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		params.gravity = Gravity.NO_GRAVITY;

		addView(layout, params);

	}
	
	/**
	 * Sets the view data from a given overlay item.
	 * 
	 * @param item - The overlay item containing the relevant view data 
	 * (title and snippet). 
	 */
	public void setData(Item item) {
		LogUtil.log("BalloonOverlayView", "setData");
		layout.setVisibility(VISIBLE);
		if (item.getTitle() != null) {
			title.setVisibility(VISIBLE);
			storeId = item.getStoreId();
			String titleText = item.getTitle().toString().trim();
			if(titleText.length() > 25)
				titleText = titleText.substring(0, 22) + "...";
			title.setText(titleText);
		} else {
			title.setVisibility(GONE);
		}
		if (item.getSnippet() != null) {
			snippet.setVisibility(VISIBLE);
			snippet.setText(item.getSnippet().toString().trim());
		} else {
			snippet.setVisibility(GONE);
		}
	}
	
	
	
	//Create an anonymous implementation of OnClickListener
	private OnClickListener navButtonClickListener = new OnClickListener() {
	    public void onClick(View v) {
	    	storeService= (StoreService)myContext.getApplicationContext();
	    	StoreVO storeObj = storeService.getStore(storeId);
	    	if(storeObj.address != null) {
	    		String addressLine = storeObj.address;
	    		if(storeObj.city != null)
	    			addressLine += "+" + storeObj.city;
	    		
	    		if(storeObj.state != null)
	    			addressLine += "+" + storeObj.state;
	    		
	    		if(storeObj.zip != null)
	    			addressLine += "+" + storeObj.zip;
	    		
	    		addressLine = addressLine.replaceAll(" ", "+");
	    		LogUtil.log("navButtonClickListener", addressLine);
	    		/*
	    		intent = new Intent(android.content.Intent.ACTION_VIEW, 
		    	Uri.parse("google.navigation:q=" + addressLine));
		    	*/
	    		Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
	    		Uri.parse("http://maps.google.com/maps?daddr=" + addressLine));
		    	myContext.startActivity(intent);
	    		
	    	}
	    }
	};
	
	//Create an anonymous implementation of OnClickListener
	private OnClickListener phoneButtonClickListener = new OnClickListener() {
	    public void onClick(View v) {
	    	//LogUtil.log("BalloonOverlayView", "onClick " + title.getText());
	    	storeService= (StoreService)myContext.getApplicationContext();
	    	String phone = storeService.getPhone(storeId);
	    	intent = new Intent(Intent.ACTION_DIAL);
	    	intent.setData(Uri.parse(phone));
        myContext.startActivity(intent);
      }
	};
	
	//Create an anonymous implementation of OnClickListener
	private OnClickListener reviewsClickListener = new OnClickListener() {
	    public void onClick(View v) {
	    	Intent myIntent = new Intent(myContext, ReviewActivity.class);
	    	myIntent.putExtra(StoreService.ACTIVITY_KEY, DetroitChowActivity.REVIEWS);
	    	myIntent.putExtra(StoreService.STREAM_VALUE, String.valueOf(storeId));
	    	myContext.startActivity(myIntent);
	    	
	    }
	};	
	
	//Create an anonymous implementation of OnClickListener
	private OnClickListener webClickListener = new OnClickListener() {
	    public void onClick(View v) {
	    	//LogUtil.log("BalloonOverlayView", "onClick " + title.getText());
	    	storeService= (StoreService)myContext.getApplicationContext();
	    	String url = storeService.getWeb(storeId);
	    	if(url != null && url.length() > 0) {
	    		intent = new Intent(Intent.ACTION_VIEW);
		    	intent.setData(Uri.parse(url));
	        myContext.startActivity(intent);
	    	}else{
	    		
	    		int duration = Toast.LENGTH_SHORT;
	    		Toast toast = Toast.makeText(myContext, "Web Site not available", duration);
	    		toast.show();
	    		
	    	}
	    	
	    }
	};	

}